package uk.ac.aber.paws.server.db;

import java.sql.DriverManager;

import java.sql.*;
import java.util.logging.Level;

import uk.ac.aber.paws.server.config.ConfigManager;
import uk.ac.aber.paws.server.config.Constants;
import uk.ac.aber.paws.server.core.PAWSServer;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * ParentDBOps.java
 * 
 * A parent class dealing with generic operations for database connections.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ParentDBOps extends Thread {

	/**
	 * Stores the reference to the database connection
	 */
	protected Connection conn;
	
	/**
	 * Creates the connection to the database
	 * @param dbObject The parameters for creating the database connection
	 */
	protected void connectDB(DBConnectionObject dbObject) {

		try {
			String url = null;
			
			String dbType = dbObject.getDBType();
			
			// simple authentication....
			if (dbObject.getDBUsername() != null && dbObject.getDBPassword() != null) {
				if (dbType.equals(Constants.MYSQLDB)) {
					url = "jdbc:mysql://"+dbObject.getDBAddress1()+":"+dbObject.getDBPort()+"/"+dbObject.getDBName();
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				}
				if (dbType.equals(Constants.SQLSERVERDB)) {
					url = "jdbc:sqlserver://"+dbObject.getDBAddress1()+":"+dbObject.getDBPort()+";databaseName="+dbObject.getDBName()+";failoverPartner="+dbObject.getDBAddress2()+"";
					Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
				}
				
				conn = DriverManager.getConnection(url, dbObject.getDBUsername(), dbObject.getDBPassword());
			}
			
			// windows authentication...
			else {

				if (dbType.equals(Constants.MYSQLDB)) {
					url = "jdbc:mysql://"+dbObject.getDBAddress1()+"/"+dbObject.getDBName();
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				}
				if (dbType.equals(Constants.SQLSERVERDB)) {
					url = "jdbc:sqlserver://"+dbObject.getDBAddress1()+":"+Integer.parseInt(ConfigManager.getProperty("app.db.sqlserverport"))+";databaseName="+dbObject.getDBName()+";failoverPartner="+dbObject.getDBAddress2()+";integratedSecurity=true";
					Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
				}
				
				conn = DriverManager.getConnection(url);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {

		}
	}
	
	/**
	 * Disconnects the active database connection
	 */
	protected void disconnectDB() {

		if (conn != null) {
			
        	try {
        		conn.close ();
        	}
        	catch (Exception e) { 
        		/* ignore close errors */ 
        	}
	    }
	}
}
